import axios from 'axios'
import config from '@/config'
import bus from '@/assets/utils/bus'

const isProduction = process.env.NODE_ENV === 'production'
const BASE_URL = isProduction ? config.PRO_BASE_URL : config.TEST_BASE_URL

const instance = axios.create({
  baseURL: BASE_URL,
  timeout: config.TIMEOUT,
  headers: config.POST_HEADERS
});


instance.interceptors.request.use(
  (request) => {
    console.log(request)
    return request
  },
  (error) => {
    return Promise.reject(error)
  }
)

export const post = (url, params = {}) => {
  const data = params
  return instance
    .post(url, data)
    .then((res) => {
      if (res.status !== 200) {
        // 浏览器环境弹出报错信息
        if (typeof window !== 'undefined') {
          bus.$message.error(res.messgae)
        }
      } else {
        return res.data
      }
    })
    .catch((err) => {
      console.log(err)
      // 浏览器环境弹出报错信息
      if (typeof window !== 'undefined') {
        bus.$message.error('网络异常')
      }
    })
}
